extends Node
class_name MushroomLogic

# 配置
@export var mushroom_config: MushroomConfig

# 内部函数
var is_on_seat: bool = false
var can_drag: bool = true



enum mushroom_stat{
	Waiting,
	Ongoing,
	Seating,
	Falling,
	Oncontrol,
	Trip,
}

var current_stat: int = mushroom_stat.Waiting


# 信号
# 向管理器发起
signal apply_for_seat(Mushroom: Node)
signal mushroom_seating(Mushroom: Node)
signal cancel_line(mushroom: Node)

# 获取引用
@onready var animator = $CharacterBody2D/Mushroom_animator

# 初始化
func _ready():
	add_to_group("mushroom")
	print(current_stat)

# 状态判断
func _process(delta: float) -> void:
	mushroom_stat_manage()

# 根据状态执行不同的函数
func mushroom_stat_manage():
	match current_stat:
		mushroom_stat.Waiting:
			_on_mushroom_waiting()
		mushroom_stat.Ongoing:
			_on_mushroom_ongoing()
		mushroom_stat.Falling:
			_on_mushroom_falling()
		mushroom_stat.Seating:
			_on_mushroom_seating()
		mushroom_stat.Oncontrol:
			_on_mushroom_oncontrol()
		mushroom_stat.Trip:
			_on_mushroom_trip()
	
# 当等待中时，向管理器发起排队申请
func _on_mushroom_waiting():
	can_drag = true
	apply_for_seat.emit(self)
	animator.play("walk")
	return


# 当处于被控制状态中，向管理器发起退出申请
func _on_mushroom_oncontrol():
	can_drag = true
	cancel_line.emit(self)
	animator.play("drag")
	return

func _on_mushroom_falling():
	animator.play("fall")
	can_drag = true


# 当处于远征状态，向管理器发起退出申请
func _on_mushroom_trip():
	can_drag = false
	cancel_line.emit(self)
	return
	
# 当处于已分配座位时，向座位发出信号
func _on_mushroom_ongoing():
	can_drag = true
	mushroom_seating.emit(self)
	return
	
# 当已经坐好
func _on_mushroom_seating():
	can_drag = true
	return

# 状态转移方法
# 由木桩管理器来设置蘑菇人等待状态
func set_mushroom_stat_waiting():
	current_stat = mushroom_stat.Waiting

# 由木桩管理器来设置蘑菇人前往座位状态
func set_mushroom_stat_ongoing():
	current_stat = mushroom_stat.Ongoing

# 由座位设置蘑菇人坐好状态
func set_mushroom_stat_seating():
	current_stat = mushroom_stat.Seating

# 由蘑菇人碰撞和输入管理器设置被控制状态
func set_mushroom_stat_oncontrol():
	current_stat = mushroom_stat.Oncontrol

# 由蘑菇人characterbody2D判断是否下落：
func set_mushroom_stat_falling():
	current_stat = mushroom_stat.Falling
	

# 由远征出发点设置处于旅途中状态
func set_mushroom_stat_trip():
	current_stat = mushroom_stat.Trip
